package com.dianxun.pagerecyclerviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.dianxun.page.PagerGridLayoutManager;
import com.dianxun.page.PagerGridSnapHelper;
import com.dianxun.pagerecyclerviewdemo.adapter.CommonAdapter;
import com.dianxun.pagerecyclerviewdemo.adapter.ViewHolder;
import com.dianxun.pagerecyclerviewdemo.entity.Entrance;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试
 *
 * @author fan
 * @date 2018/11/6
 */

public class NextActivity extends AppCompatActivity implements PagerGridLayoutManager.PageListener {

    private RecyclerView mRecyclerView;

    private LinearLayout mPoints;

    private PagerGridLayoutManager mLayoutManager;

    private List< Entrance > mData = new ArrayList<>();

    private CommonAdapter< Entrance > mAdapter;

    private ImageView[] ivPoints;

    int rows = 3;

    int columns = 2;

    private boolean isFirst = true;

    @Override
    protected void onCreate ( @Nullable Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_next );

        mRecyclerView = findViewById( R.id.recycler_view );
        mPoints = findViewById( R.id.points );

        // 1.分页布局管理器
        mLayoutManager = new PagerGridLayoutManager( rows, columns, PagerGridLayoutManager
                .HORIZONTAL );

        //
        mRecyclerView.setLayoutManager( mLayoutManager );
        // 设置页面变化监听器
        mLayoutManager.setPageListener( this );

        //2。设置滚动辅助工具
        PagerGridSnapHelper pageSnapHelper = new PagerGridSnapHelper();
        pageSnapHelper.attachToRecyclerView( mRecyclerView );

        mData.add( new Entrance( "充值", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "查余额", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "交易查询", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "撤销订单", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "修改密码", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "结算", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "加油", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "聚会", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "Happy", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "KTV", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "CCV", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "充值", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "查余额", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );
        mData.add( new Entrance( "交易查询", R.mipmap.index_menu_icon8, R.mipmap.index_menu_bg8 ) );

        mRecyclerView.setAdapter( mAdapter = new CommonAdapter< Entrance >( this, mData, R.layout.item_entrance ) {
            @Override
            public void convert ( int position, ViewHolder helper, Entrance item ) {
                LinearLayout recharge = helper.getView( R.id.recharge );
                recharge.setBackgroundResource( item.getBackground() );
                helper.setText( R.id.name, item.getName() );
                helper.setImageResource( R.id.photo, item.getRes() );
            }
        } );

        mAdapter.setOnItemClickListener( ( view, position ) -> {
            Log.e( "点击了哪个？", "" + position );
            Toast.makeText( NextActivity.this, "" + position, Toast.LENGTH_SHORT ).show();
        } );

        mAdapter.setOnItemLongClickLitener( new CommonAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClickListener ( View view, int position ) {
                mAdapter.removeItem( position );
            }
        } );
        point();
    }

    private void point () {
        int pageCount = (int) Math.ceil( mData.size() * 1.0 / ( rows * columns ) );
        ivPoints = new ImageView[pageCount];
        ImageView imageView;
        for ( int i = 0; i < ivPoints.length; i++ ) {
            imageView = new ImageView( this );
            //设置图片的宽高
            if ( i == 0 ) {
                imageView.setImageResource( R.drawable.item_dot_blue );
            } else {
                imageView.setImageResource( R.drawable.item_dot_gray );
            }
            ivPoints[i] = imageView;
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) );
            layoutParams.setMargins( 18, 0, 18, 0 );
            layoutParams.gravity = Gravity.CENTER;
            mPoints.addView( imageView, layoutParams );
        }

    }


    @Override
    public void onPageSizeChanged ( int pageSize ) {
        //页面总数量变化
        if ( isFirst ) {
            isFirst = false;
            return;
        }
        mPoints.removeAllViews();
        ivPoints = null;
        point();
    }

    @Override
    public void onPageSelect ( int pageIndex ) {
        //页面被选中
        setImageBackground( pageIndex );
        Log.e( "onPageSelect", "" + pageIndex );
    }

    /**
     * 改变点点点的切换效果
     *
     * @param selectItems ViewPager position
     */
    private void setImageBackground ( int selectItems ) {
        for ( int i = 0; i < ivPoints.length; i++ ) {
            if ( i == selectItems ) {
                ivPoints[i].setImageResource( R.drawable.item_dot_blue );
            } else {
                ivPoints[i].setImageResource( R.drawable.item_dot_gray );
            }
        }
    }
}
